package com.zjitc.service.imp;

import com.github.pagehelper.PageHelper;
import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import com.zjitc.utils.MailUtil;
import java.util.List;
import javax.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by LWW on 2018/4/19.
 */
@Service
public class UserServiceImp implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User login(String username, String password) {
        return userDao.login(username,password);
    }

  @Override
  public boolean register(User user) throws MessagingException {
    if(userDao.register(user)){
      String emailMsg="欢迎你注册成为我们的其中一员，"
          + "<a href='http://localhost:8080/user/active.do?code="+user.getCode()+"'>请激活，点此激活</a>";
      MailUtil.sendMail(user.getEmail(),emailMsg);
    }
    return true;
  }
  //@Override
  //public boolean register(User user) {
  //  User username=userDao.findUsername(user.getUsername());
  //  if(username!=null){
  //    return false;
  //  }
  //  boolean isRegister=userDao.register(user);
  //  if(isRegister){
  //    String emailMsg="欢迎你注册成为我们的其中一员，" +
  //        "<a href='http://localhost:8080/user/active.do?code="+user.getCode()+"'>请激活，点此激活</a>";
  //      MailUtil mailUtil = new MailUtil();
  //    try {
  //      mailUtil.sendMail(user.getEmail(),emailMsg);
  //    } catch (MessagingException e) {
  //      e.printStackTrace();
  //    }
  //  }
  //  return false;
  //}
  //
  @Override
  public User active(String code) {
      User active=userDao.active(code);
      if(active==null){
        return null;
      }
      active.setState(1);
      userDao.updateState(active);
      return active;
  }

  @Override
  public User findUser(String uid) {
    return userDao.findUser(uid);
  }

  @Override
  public List<User> findAllUser(Integer pageNum, Integer count) {
    PageHelper.startPage(pageNum,count);
    List<User> users=userDao.findAllUser();
    return  users;
  }



  @Override
  public void editUser(User user) {
    userDao.editUser(user);
  }

  @Override
  public void deleteUser(String uid) {
    userDao.deleteUser(uid);
  }

}
